//
//  read_steady_state.cpp
//  EMT_V2_Crisis
//
//  Created by Duong Ngo on 6/6/17.
//  Copyright © 2017 Duong Ngo. All rights reserved.
//

#include "read_steady_state.hpp"
#include "global.hpp"
#include <fstream>
#include <math.h>
#include <iomanip>

void read_steady(std::vector<double>& ss)
{
    std::ifstream file;
    file.open("//Users//duongngo//Documents//Research//E-Monetary Theory//Paper_and_code//EMT_V6//C code//Output//EMT_steady.txt");
    int j;
    file >> j;
    for (int i=0; i<ss.size(); ++i){
        file >> j >> ss[i];
    }
    file.close();
    
    // Change some multipliers
    //ss[original_map.at("s.mur")] = pow(ss[original_map.at("s.mur")], 0.5);
    ss[original_map.at("s.muc")] = ss[original_map.at("s.n")]+ (1-kappa)*ss[original_map.at("s.bh")]- ss[original_map.at("s.m")];
    ss[original_map.at("s.muc")] = -ss[original_map.at("s.muc")];
    ss[original_map.at("s.mur")] = pow(ss[original_map.at("s.mur")], 0.5);
    ss[original_map.at("s.etaz")] = pow(ss[original_map.at("s.etaz")], 0.5);
    
    // Other steady state variable not in file
    ss[original_map.at("s.Rn")]= Rn;
    ss[original_map.at("s.x")]= 0;
    ss[original_map.at("s.w")]= (1-ss[original_map.at("s.pm")])*ss[original_map.at("s.y")];
    ss[original_map.at("s.up")]= beta_h/(1-beta_h)*ss[original_map.at("s.w")];
    
    // Maximum capital requirement
    double x= 1-(ss[10]-ss[9])/ss[11];
    printf("x= %.5f\n", x);
    
}

//---------------------------------------------------------------------------------------------------------


void initial_guess_as_steady_state (const std::vector<double>& ss, std::vector<double>& x_init)
{
    for (int t=0; t<=T; ++t){
        for (int i=0; i<var_foc; ++i){
            x_init[t*var_foc+ i] = ss[i];
        }
    }
}

//---------------------------------------------------------------------------------------------------------

void print_vector (const std::vector<double>& x)
{
    int col =5;
    int row = x.size()/col + 1;
    for (int i=0; i<row; ++i){
        for (int j=0; j<col; ++j){
            if (i*col+j < x.size()){
                printf("x[%2u]= %.5f;   ", i*col+j, x[i*col+j]);
            }
        }
        printf("\n");
    }
}

//---------------------------------------------------------------------------------------------------------

void store_output (const std::vector<double>& result, const double& chi)
{
    std::ofstream file;
    file.open("//Users//duongngo//Documents//Research//E-Monetary Theory//Paper_and_code//EMT_V6//C code//Output//EMT_ms_rule.txt");
    
    file << T*var_foc << " " <<  chi << std::endl;
    for (int t=0; t<=T; ++t){
        for (int i=0; i<var_foc; ++i){
            file << t << " " << i << " " << std::setprecision(25)<< result[t*var_foc+i] << std::endl;
        }
    }
    
    file.close();
}

//---------------------------------------------------------------------------------------------------------

void read_guess_from_previous_homotopy (const std::string& filename, std::vector<double>& x_init)
{
    std::ifstream file;
    file.open(filename.c_str());
    
    int n; double chi;
    file >> n >> chi;
    
    int u, v;
    for (int t=0; t<T+1; ++t){
        for (int i=0; i<var_foc; ++i){
            file >> u >> v >> x_init[t*var_foc+i];
        }
    }
    file.close();
}



